package routes

import (
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/thinkerou/favicon"
	"time"
)

func RouteInit() *gin.Engine {
	r := gin.Default()
	r.Delims("<<", ">>") //这个必须放在加载模板前边
	//加载js等静态文件,注意放在需要加载中间件的前边，因为中间件里面做了权限认证。如果在权限认证后面静态资源就不能加载了
	r.Static("/static", "./static")
	r.Use(favicon.New("./static/favicon/favicon.ico")) //网站页面图标
	r.LoadHTMLGlob("templates/**/*")                   //全局加载前端页面
	//解决跨越问题
	r.Use(cors.New(cors.Config{
		// 允许所有域进行跨域请求
		AllowOriginFunc: func(origin string) bool {
			return true
		},
		AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, // 允许的方法列表
		AllowHeaders:     []string{"Content-Type", "Authorization"},                    // 允许的请求头列表
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
		MaxAge:           12 * time.Hour,
	}))
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
	return r
}
